<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="账号" prop="account">
        <el-input v-model="dataForm.account" placeholder="帐号"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
      </el-form-item>
      <!-- <el-form-item label="密码" prop="password" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.password" type="password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="comfirmPassword" :class="{ 'is-required': !dataForm.id }">
        <el-input v-model="dataForm.comfirmPassword" type="password" placeholder="确认密码"></el-input>
      </el-form-item> -->
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
      </el-form-item>      
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="角色" size="mini" prop="roleId">
        <el-radio-group v-model="dataForm.roleId">
          <el-radio v-for="role in roleList" :key="role.id" :label="role.id">{{ role.roleName }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="等级" size="mini" prop="levelId">
        <el-radio-group v-model="dataForm.levelId">
          <el-radio v-for="level in levelList" :key="level.id" :label="level.id">{{ level.name }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="commonStatus">
        <el-radio-group v-model="dataForm.commonStatus">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="2">禁用</el-radio>          
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isEmail, isMobile } from '@/utils/validate'
  export default {
    data () {
      var validatePassword = (rule, value, callback) => {
        if (!this.dataForm.id && !/\S/.test(value)) {
          callback(new Error('密码不能为空'))
        } else {
          callback()
        }
      }
      var validateComfirmPassword = (rule, value, callback) => {
        if (!this.dataForm.id && !/\S/.test(value)) {
          callback(new Error('确认密码不能为空'))
        } else if (this.dataForm.password !== value) {
          callback(new Error('两次密码不一致'))
        } else {
          callback()
        }
      }
      var validateEmail = (rule, value, callback) => {
        if (!isEmail(value)) {
          callback(new Error('邮箱格式错误'))
        } else {
          callback()
        }
      }
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        roleList: [],
        levelList: [],
        defaultIcon: '',
        dataForm: {
          id: '',
          account: '',
          nickname: '',
          password: '',
          comfirmPassword: '',
          birthday: '',
          city: '',
          email: '',
          gender: '',
          iconUrl: '',
          levelId: '',
          mobile: '',
          roleId: '',
          selfSign: '',
          commonStatus: ''
        },
        dataRule: {
          account: [
            { required: true, message: '账号不能为空', trigger: 'blur' }
          ],
          password: [
            { validator: validatePassword, trigger: 'blur' }
          ],
          comfirmPassword: [
            { validator: validateComfirmPassword, trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' },
            { validator: validateEmail, trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ],
          roleId: [
            { required: true, message: '角色不能为空', trigger: 'blur' }
          ],
          levelId: [
            { required: true, message: '等级不能为空', trigger: 'blur' }
          ],
          commonStatus: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl('/admin-login/role/queryAll'),
          method: 'get'
        }).then(({data}) => {
          this.roleList = data.data
          this.$http({
            url: this.$http.adornUrl('/mall-user/user/level/queryList'),
            method: 'get'
          }).then(({data}) => {
            this.levelList = data.data
          })
        }).then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }).then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/mall-user/user/queryUserById/${this.dataForm.id}`),
              method: 'get'
            }).then(({data}) => {
              if (data.code === 200) {
                this.dataForm.account = data.data.account
                this.dataForm.nickname = data.data.nickname
                this.dataForm.password = data.data.password
                this.dataForm.mobile = data.data.mobile
                this.dataForm.email = data.data.email
                this.dataForm.roleId = data.data.roleId
                this.dataForm.levelId = data.data.levelId
                this.dataForm.iconUrl = data.data.iconUrl
                this.dataForm.commonStatus = data.data.commonStatus
              }
            })
          }
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/admin-interfaces/file/defaultIcon`),
            method: 'get'
          }).then(({data}) => {
            if (data.code === 200) {
              this.defaultIcon = data.data
            }
          })
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/mall-user/user/saveOrUpdateUser'),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'account': this.dataForm.account,
                'nickname': this.dataForm.nickname,
                'password': this.dataForm.password,
                'mobile': this.dataForm.mobile,
                'email': this.dataForm.email,
                'levelId': this.dataForm.levelId,
                'roleId': this.dataForm.roleId,
                'iconUrl': this.dataForm.iconUrl || this.defaultIcon,
                'commonStatus': this.dataForm.commonStatus
              })
            }).then(({data}) => {
              if (data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.message)
              }
            })
          }
        })
      }
    }
  }
</script>
